﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ZK.YHP_TransManage.Test
{
    public partial class WindowsServiceInstall快捷键 : Form
    {
        public WindowsServiceInstall快捷键()
        {
            InitializeComponent();
            textBox1.ReadOnly = true;
            textBox2.ReadOnly = true;
        }
        /// <summary>
        /// 注册服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(textBox1.Text.Trim()) || string.IsNullOrWhiteSpace(textBox2.Text.Trim()))
            {
                Common.ShowBlackBox.WriteLine("The path cannot be empty");
                return;

            }
            Common.ShowBlackBox.WriteLine("install and begin ");
            RunCmd();
        }

        private void RunCmd(bool res = true)
        {
            try
            {
                System.Diagnostics.Process process = new System.Diagnostics.Process();
                process.StartInfo.FileName = "cmd.exe";
                process.StartInfo.UseShellExecute = false;     //是否使用操作系统shell启动
                process.StartInfo.CreateNoWindow = true;        //不显示程序窗口
                process.StartInfo.RedirectStandardOutput = true;  //由调用程序获取输出信息
                process.StartInfo.RedirectStandardInput = true;  //接受来自调用程序的输入信息
                process.StartInfo.RedirectStandardError = true;  //重定向标准错误输出
                                                                 //process.StartInfo.UserName = "Administrator";
                                                                 //System.Security.SecureString password = new System.Security.SecureString();
                                                                 //password.AppendChar('1');
                                                                 //password.AppendChar('2');
                                                                 //password.AppendChar('3');
                                                                 //process.StartInfo.Password = password;

                string command = textBox1.Text + " " + textBox2.Text;
                if (!res)
                {
                    command = textBox1.Text + " -u " + textBox2.Text;
                }
                process.Start();
                process.StandardInput.WriteLine(command + "&exit");   //向cmd窗口发送输入信息，&exit意思为不论command命令执行成功与否，接下来都执行exit这句
                process.StandardInput.AutoFlush = true;

                string output = process.StandardOutput.ReadToEnd();  //获取cmd的输出信息
                process.WaitForExit();         //等待程序执行完退出进程
                process.Close();
                process.Dispose();
                Common.ShowBlackBox.WriteLine(output);

                // MessageBox.Show("command命令：" + output);
            }
            catch (Exception a)
            {
                MessageBox.Show(a.Message);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.ShowDialog();
            Common.ShowBlackBox.WriteLine("open install file window");
            textBox1.Text = openFileDialog.FileName;
            Common.ShowBlackBox.WriteLine("install file path is " + openFileDialog.FileName);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.ShowDialog();
            Common.ShowBlackBox.WriteLine("open window service  file window");
            textBox2.Text = openFileDialog.FileName;
            Common.ShowBlackBox.WriteLine("window service path is " + openFileDialog.FileName);
        }
        //unstall
        private void button2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(textBox1.Text.Trim()) || string.IsNullOrWhiteSpace(textBox2.Text.Trim()))
            {
                Common.ShowBlackBox.WriteLine("The path cannot be empty");
                return;
            }
            Common.ShowBlackBox.WriteLine("uninstall ~~~~");
            RunCmd(false);
        }
    }
}
